angular.module('menu.menubar', ['menu'])

// The loginToolbar directive is a reusable widget that can show login or logout buttons
// and information the current authenticated user
.directive('menubar', ['menu', '$q', function(menu, $q) {
    var directive = {
        templateUrl: 'menu/menu.tpl.html',
        restrict: 'AE',
        replace: true,
        scope: {
            list : '@' // 单项数据绑定
        },
        link: function($scope, $element, $attrs) {
            (function () {
                var a = angular.element;

                var c = a("#sidebar").hasClass("menu-min");
                a("#sidebar-collapse").on('click', function() {
                    c = a("#sidebar").hasClass("menu-min");
                    ace.settings.sidebar_collapsed(!c);
                })

                var b = navigator.userAgent.match(/OS (5|6|7)(_\d)+ like Mac OS X/i);
                a(".nav-list").on('click', function(h) {
                    var g = a(h.target).closest("a");
                    if (!g || g.length === 0) {
                        return;
                    }
                    c = a("#sidebar").hasClass("menu-min");
                    if (!g.hasClass("dropdown-toggle")) {
                        if (b) {
                            //document.location = g.attr("href");
                        } else {
                            //alert('打开链接');
                        }
                        a('.nav-list li').removeClass('active');
                        g.parent().addClass('active');
                        return;
                    }

                    var f = g.next().get(0);
                    if (!a(f).is(":visible")) {
                        var d = a(f.parentNode).closest("ul");
                        if (c && d.hasClass("nav-list")) {
                            return;
                        }
                        d.find("> .open > .submenu").each(function() {
                            if (this != f && !a(this.parentNode).hasClass("active")) {
                                a(this).slideUp(200).parent().removeClass("open");
                            }
                        });
                    } else {
                    }
                    if (c && a(f.parentNode.parentNode).hasClass("nav-list")) {
                        return false;
                    }
                    a(f).slideToggle(200).parent().toggleClass("open");
                    return false;
                });
            }) ();

        }
    };
    return directive;
}]);
